package server

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"kratos_cron_task/internal/service"
	"kratos_cron_task/pkg/task1"
)

func NewTaskServer(service *service.CronService, logger log.Logger) *task1.Server {
	helper := log.NewHelper(logger)
	ctx := context.Background()

	//  1、执行定时任务的方法定义
	fn1 := func() {
		err := service.RunCronTask1Service(ctx)
		if err != nil {
			helper.Error(err)
			return
		}
	}

	srv := task1.NewServer(
		task1.Logger(logger),
		task1.WithContext(ctx),

		// Notice crontab在线工具: https://tool.lu/crontab/
		// 每1分钟的开始执行一次
		task1.RegisterFunc("*/1 * * * *", "cronTest1", func() {
			fn1()
		}),
	)

	// Notice 项目重启会执行～～
	//go fn1()

	return srv
}
